import { _decorator, Component, Node, Label, view, UITransform, tween, Vec3, Tween } from 'cc';
import { UIBase } from './ui_base';
import { UIManagerDefines } from '../../farmework/ui_manager_define';
const { ccclass, property } = _decorator;

const PosY = 200;
/**
 * 跑马灯节点
 */
@ccclass('UIMarqueeBase')
export class UIMarqueeBase extends UIBase {
    @property(Label)
    contentLabel: Label | null = null;

    show (content: string) {
        Tween.stopAllByTarget(this.node);

        if (this.contentLabel) {
            this.contentLabel!.string = content;
            this.contentLabel.updateRenderData();
        }
        // 播放动画
        let size = view.getVisibleSize();
        let width = this.node.getComponent(UITransform)!.width;
        let posX = size.width / 2 + width / 2;

        this.node.position.set(posX, PosY);

        tween(this.node).to((width + size.width) / UIManagerDefines.MARQUEE_LABEL_SPEED, { position: new Vec3(-size.width / 2 - width, PosY, 0) }).call(() => {
            this.node.active = false;
        }).start();
    }
}

